Imports System.IO

Partial Class diashow
    Inherits System.Web.UI.UserControl
    Dim mVerz As String
    Private images() As FileInfo
    Private imgIndex As Integer = 0
    Property Verz() As String
        Get
            Return mVerz
        End Get
        Set(ByVal value As String)
            mVerz = value
        End Set
    End Property
    Public Sub Init3()
        Dim Datei As String
        Datei = Path.GetFileName(Verz)
        Dim lVerz As String = Path.GetDirectoryName(Verz)
        Dim dirInfo As New DirectoryInfo(lVerz)
        Me.images = FilterForImages(dirInfo.GetFiles())

        If Datei <> "" Then ' Index des Bildes suchen ( aus Thumbs )
            For Me.imgIndex = 0 To images.Length - 1
                If Me.images(imgIndex).Name = Datei Then Exit For
            Next
        Else
            If Not Request.QueryString("N") Is Nothing AndAlso IsNumeric(Request.QueryString("N")) Then
                Me.imgIndex = CInt(Request.QueryString("N"))
            End If
        End If

        Session("images") = Me.images
        Session("imgIndex") = Me.imgIndex
        Session("lVerz") = lVerz
    End Sub

    Public Sub show_files()
        If Session("lVerz") = Nothing Then
            Init3()
        End If
        Dim lVerz As String = Session("lVerz")
        Me.images = Session("images")
        Me.imgIndex = Session("imgIndex")

        'panAdmin.Visible = Roles.IsUserInRole("admin")

        If lVerz.Substring(lVerz.Length - 1) <> "\" Then lVerz += "\"
        Dim s As String
        s = lVerz & "\" & images(Me.imgIndex).Name

        Bild.ImageUrl = s.Replace(MapPath("~/"), "~/")  '   , "  '"~/userfiles/image/Galerie/FamilieFreunde/999%20Pfalz%20mit%20Toni%202004/PICT0056.JPG" 's.Replace("%20", " ")

        GallKomm.Text = MasterCms.CTools.ReadKommentar(lVerz & "kommentar.txt", False)
        labKommentar.Text = MasterCms.CTools.ReadKommentar(lVerz & images(imgIndex).Name.ToUpper.Replace(".JPG", ".TXT"), False)
        labPos.Text = "( " & Me.imgIndex + 1 & " von " & Me.images.Length & " )"
    End Sub

    Function FilterForImages(ByVal images() As FileInfo) As FileInfo()
        Dim newImages As New ArrayList(images.Length)

        Dim i As Integer
        For i = 0 To images.Length - 1
            If Path.GetExtension(images(i).Name.ToLower()) = ".jpg" OrElse _
               Path.GetExtension(images(i).Name.ToLower()) = ".jpeg" OrElse _
               Path.GetExtension(images(i).Name.ToLower()) = ".png" OrElse _
               Path.GetExtension(images(i).Name.ToLower()) = ".gif" Then
                newImages.Add(images(i))
            End If
        Next
        Return CType(newImages.ToArray(GetType(FileInfo)), FileInfo())
    End Function

    Protected Sub imgKommentar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Response.Redirect("admin/kommentar.aspx?Datei=" & MapPath(Verz) & images(imgIndex).Name.ToUpper.Replace(".JPG", ".txt"))
    End Sub

    Protected Sub imgDel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        File.Delete(MapPath(Verz) & images(imgIndex).Name)
    End Sub

    Public Event IndexClick()

    Protected Sub but_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

        Me.imgIndex = Session("imgIndex")
        Me.images = Session("images")
        Select Case sender.CommandArgument.ToString
            Case "E" : Me.imgIndex = 1
            Case "V"
                If Me.imgIndex > Me.images.GetLowerBound(0) Then
                    Me.imgIndex -= 1
                Else
                    Me.imgIndex = Me.images.GetUpperBound(0)
                End If
            Case "I" : RaiseEvent IndexClick()
            Case "N"
                If Me.imgIndex < Me.images.GetUpperBound(0) Then
                    Me.imgIndex += 1
                Else
                    Me.imgIndex = Me.images.GetLowerBound(0)
                End If
            Case "L" : Me.imgIndex = Me.images.Length - 1
        End Select
        Session("imgindex") = Me.imgIndex
        show_files()
    End Sub
End Class
